from django.db import models

# Create your models here.
class Category(models.Model):
    name = models.CharField(max_length=30,unique=True,verbose_name='商品类别')
    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '类别'
        verbose_name_plural = verbose_name
        ordering = ['id']

class Goods(models.Model):
    name=models.CharField(max_length=100,verbose_name='商品名称')
    desc = models.CharField(max_length=100,verbose_name='商品描述')
    price = models.DecimalField(max_digits=5,decimal_places=2,verbose_name='现价')
    oldprice=models.DecimalField(max_digits=5,decimal_places=2,verbose_name='原价')
    category=models.ForeignKey(Category,on_delete=models.CASCADE)

    def __str__(self):
        return self.name
    def getImg(self):
        return self.inventory_set.first().color.value
    def getColors(self):
        colors=[]
        for inventory in self.inventory_set.all():
            color=inventory.color
            if color not in colors:
                colors.append(color)
        return colors
    def getSizes(self):
        sizes=[]
        for inventory in self.inventory_set.all():
            size=inventory.size
            if size not in sizes:
                sizes.append(size)
        return sizes




    def getDetails(self):
        import collections
        datas=collections.OrderedDict()
        for detail in self.goodsdetailsitem_set.all():
            dname = detail.name()
            if dname not in datas:
                datas[detail.name()]=[detail.values]
            else:
                datas[detail.name()].append(detail.values)
        return datas



    class Meta:
        verbose_name='商品'
        verbose_name_plural=verbose_name
        ordering=['id']




class Color(models.Model):
    name = models.CharField(max_length=20,verbose_name='颜色名称')
    value = models.ImageField(upload_to='media/color/',verbose_name='图片地址')
    def __str__(self):
        return self.name
    class Meta:
        verbose_name='颜色'
        verbose_name_plural=verbose_name
        ordering=['id']
class Size(models.Model):
    name = models.CharField(max_length=20, verbose_name='颜色尺寸')
    value = models.CharField(max_length=20,)
    def __str__(self):
        return self.name
    class Meta:
        verbose_name='尺寸'
        verbose_name_plural=verbose_name
        ordering=['id']
class GoodsDetails(models.Model):
    name=models.CharField(max_length=20,verbose_name='商品详情')
    def __str__(self):
        return self.name

    class Meta:
        verbose_name='详情'
        verbose_name_plural=verbose_name
        ordering=['id']
class GoodsDetailsItem(models.Model):
    goods = models.ForeignKey(Goods,on_delete=models.CASCADE,verbose_name='商品')
    goodsdetails=models.ForeignKey(GoodsDetails,models.CASCADE,verbose_name='商品详情')
    values = models.ImageField(upload_to='media/',verbose_name='图片地址')
    def name(self):
        return self.goodsdetails.name
    def __str__(self):
        return self.values
    class Meta:
        verbose_name='详情明细'
        verbose_name_plural=verbose_name
        ordering=['id']
class Inventory(models.Model):
    color = models.ForeignKey(Color,on_delete=models.CASCADE,verbose_name='商品颜色')
    size = models.ForeignKey(Size,on_delete=models.CASCADE,verbose_name='商品尺寸')
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE, verbose_name='商品')
    count = models.IntegerField(default=100,verbose_name='数量')
    def __str__(self):
        return self.count
    class Meta:
        verbose_name='商品库存'
        verbose_name_plural=verbose_name
        ordering=['id']
